package com.ptit.giaothong.taxi;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.SQLException;
import android.net.Uri;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TextView;

import com.ptit.giaothong.MainActivity;
import com.ptit.giaothong.R;
import com.ptit.giaothong.database.DatabaseHelper;

public class TaxiActivity extends Activity {
	private ListView listview;
	private List<itemtaxi> list;
	private Activity activity;
	public int pagageIndex = 0;
	private ItemAdapter itemAdapter = null;

	private List<itemtaxi> listSmall;

	public TaxiActivity(Activity activity) {
		this.activity = activity;

	}

	public void init(int keypage) {
		MainActivity.keypage = keypage;
		activity.setContentView(R.layout.bienbao);
		list = new ArrayList<itemtaxi>();

		DatabaseHelper myDbHelper = new DatabaseHelper(activity);
		try {
			myDbHelper.createDataBase();
		} catch (IOException ioe) {
			throw new Error("Unable to create database");
		}

		try {
			myDbHelper.openDataBase();
		} catch (SQLException sqle) {
			throw sqle;
		}

		Cursor c = null;
		c = myDbHelper.query(DatabaseHelper.TB_TAXI, null, null, null, null,
				null, null);
		int nID = 0;
		list = new ArrayList<itemtaxi>();
		if (c.moveToFirst()) {
			do {
				itemtaxi item = new itemtaxi();
				item.setId(c.getInt(1));
				item.setName(c.getString(2));
				item.setNumphone(c.getString(3));
				list.add(item);
				nID++;
			} while (c.moveToNext());
		}
		listview = (ListView) activity.findViewById(R.id.listViewbb);

		c = myDbHelper.query(DatabaseHelper.TB_LISTTINH, null, null, null,
				null, null, null);
		String[] listTinh = new String[c.getCount()];
		nID = 0;
		if (c.moveToFirst()) {
			do {
				listTinh[nID] = c.getString(1);
				nID++;
			} while (c.moveToNext());
		}

		Spinner spinner = (Spinner) activity.findViewById(R.id.spinner1);
		setAdapterSpinner(spinner, listTinh);
		spinner.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				listSmall = new ArrayList<itemtaxi>();
				for (int i = 0; i < list.size(); i++) {
					if (list.get(i).getId() == (arg2 + 1))
						listSmall.add(list.get(i));
				}
				setAdapperListview(listSmall);

			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub

			}
		});
		final TextView tView = (TextView) activity
				.findViewById(R.id.thongbaoid);
		
		final EditText ed_search = (EditText) activity
				.findViewById(R.id.txtdiemxuatphat);

		ed_search.addTextChangedListener(new TextWatcher() {
			final List<itemtaxi> listSearch = new ArrayList<itemtaxi>();

			@Override
			public void onTextChanged(CharSequence s, int start, int before,
					int count) {
				String searchString = ed_search.getText().toString().trim()
						.toLowerCase();
				if (searchString.length() > 0) {
					listSearch.clear();

					for (itemtaxi itemSearch : listSmall) {
						if (itemSearch.getName().toLowerCase()
								.contains(searchString)
								|| itemSearch.getNumphone().toLowerCase()
										.contains(searchString)) {
							listSearch.add(itemSearch);
						}
					}

					if (listSearch.size() > 0) {
						itemAdapter = new ItemAdapter(activity,
								R.layout.itemtaxi, listSearch);
					}else{
						tView.setVisibility(View.VISIBLE);
					}
				} else {
					tView.setVisibility(View.GONE);
					itemAdapter = new ItemAdapter(activity, R.layout.itemtaxi,
							listSmall);

				}
				listview.setAdapter(itemAdapter);

			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {

			}

			@Override
			public void afterTextChanged(Editable s) {

			}
		});
	}

	public void setAdapperListview(final List<itemtaxi> list) {
		itemAdapter = new ItemAdapter(activity, R.layout.itemtaxi, list);
		listview.setAdapter(itemAdapter);
		listview.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1,
					int postion, long arg3) {
				call(list.get(postion).getNumphone());
			}
		});

	}

	public void setAdapterSpinner(Spinner spinner, String[] showString) {
		ArrayAdapter<String> adapterMenhgia = new ArrayAdapter<String>(
				activity, R.layout.my_spinner_textview, showString);
		adapterMenhgia
				.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spinner.setAdapter(adapterMenhgia);

	}

	public void call(String numphone) {
		try {
			Intent callIntent = new Intent(Intent.ACTION_CALL);
			callIntent.setData(Uri.parse("tel:" + numphone));
			activity.startActivity(callIntent);
		} catch (ActivityNotFoundException activityException) {
			activityException.printStackTrace();
		}
	}

	static class ItemAdapter extends ArrayAdapter<itemtaxi> {
		private int resource;
		private LayoutInflater layoutInflater;

		public ItemAdapter(Context mContext, int resource, List<itemtaxi> models) {
			super(mContext, resource, models);
			this.resource = resource;
			layoutInflater = LayoutInflater.from(mContext);
		}

		public class ViewHolder {
			public TextView numphone;
			public TextView name;

		}

		@Override
		public View getView(final int position, View convertView,
				ViewGroup parent) {
			ViewHolder viewHolder;
			if (convertView == null) {
				viewHolder = new ViewHolder();
				convertView = layoutInflater.inflate(resource, null, true);
				viewHolder.name = (TextView) convertView
						.findViewById(R.id.nametxt);
				viewHolder.numphone = (TextView) convertView
						.findViewById(R.id.txtnumphone);
				convertView.setTag(viewHolder);
			} else {
				viewHolder = (ViewHolder) convertView.getTag();
			}
			itemtaxi item = getItem(position);
			viewHolder.name.setText(item.getName());
			viewHolder.numphone.setText("Điện thoại:" + item.getNumphone());
			return convertView;
		}
	}

}
